﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.Drawing;

namespace QExcel
{
    public class QTable : QItem
    {
        public QTable()
        {
            this.columns.Table = this;
            this.rows.Table = this;
        }

        /// <summary>
        /// 表名
        /// </summary>
        public string Name
        {
            get;
            set;
        }

        private bool exportHead = true;
        /// <summary>
        /// 是否导出表头
        /// </summary>
        public bool ExportHead
        {
            get
            {
                return exportHead;
            }
            set
            {
                exportHead = value;
            }
        }

        private int startRow = 1;
        /// <summary>
        /// 开始行
        /// </summary>
        public int StartRow
        {
            get
            {
                return startRow;
            }
            set
            {
                startRow = value;
            }
        }

        private int headFontSize = 12;
        public int HeadFontSize
        {
            get
            {
                return headFontSize;
            }
            set
            {
                headFontSize = value;
            }
        }

        private int startColumn = 1;
        /// <summary>
        /// 开始列
        /// </summary>
        public int StartColumn
        {
            get
            {
                return startColumn;
            }
            set
            {
                startColumn = value;
            }
        }

        private QColumns columns = new QColumns();
        /// <summary>
        /// 列集合
        /// </summary>
        public QColumns Columns
        {
            get
            {
                return columns;
            }
        }

        private QRows rows = new QRows();
        /// <summary>
        /// 行集合
        /// </summary>
        public QRows Rows
        {
            get
            {
                return rows;
            }
        }

        public override void Output()
        {
            this.FillColumnHead();
            this.FillContent();
        }

        /// <summary>
        /// 填充正文
        /// </summary>
        private void FillContent()
        {
            Worksheet workSheet = this.Sheet.WorkSeet;
            Range range = workSheet.get_Range(workSheet.Cells[StartRow + Columns.Depth, StartColumn], workSheet.Cells[StartRow + Rows.Count + Columns.Depth - 1, StartColumn + this.Columns.Count - 1]);
            object[,] objList = new object[rows.Count, columns.Count];
            foreach (QBaseRow row in this.rows.AllLeafRows)
            {
                foreach (QColumn column in this.columns.AllLeafColumns)
                {
                    if (column.LeafColumns.Count == 0)
                    {
                        objList[row.Index, column.Index] = row.GetValue(column);
                    }
                }
            }
            range.FormulaR1C1Local = objList;
            this.BorderWeight = XlBorderWeight.xlHairline;
            this.SetStyle(range);
            this.SetColumnContentBgColor(this.columns.AllLeafColumns, workSheet);
            this.SetRowBgColor(this.rows.Rows, workSheet);
            Range selectRange = (Range)workSheet.Cells[startRow + columns.Depth, startColumn];
            selectRange.Select();
        }

        /// <summary>
        /// 填充表头
        /// </summary>
        private void FillColumnHead()
        {
            Worksheet workSheet = this.Sheet.WorkSeet;
            foreach (QColumn clumn in this.Columns.Columns)
            {
                clumn.Sheet = this.Sheet;
                clumn.Table = this;
                clumn.Output(this.StartRow, this.StartColumn, this.Columns.Depth);
            }
        }

        /// <summary>
        /// 获取所有叶子列的正文背景色
        /// </summary>
        /// <param name="list"></param>
        private void SetColumnContentBgColor(List<QColumn> columns, Worksheet workSheet)
        {
            foreach (QColumn col in columns)
            {
                if (col.ContentBgColor.ToArgb() != Color.FromArgb(0, 0, 0, 0).ToArgb())
                {
                    Range countRange = workSheet.get_Range(workSheet.Cells[startRow + col.Depth, col.Index + startColumn], workSheet.Cells[startRow + col.Depth + this.rows.Count, col.Index + startColumn]);
                    countRange.Interior.Color = ColorTranslator.ToOle(col.ContentBgColor);
                }
            }
        }

        /// <summary>
        /// 设置行色
        /// </summary>
        /// <param name="rows"></param>
        private void SetRowBgColor(List<QBaseRow> rows, Worksheet workSheet)
        {
            foreach (QBaseRow row in rows)
            {
                if (QSheet.RowStyles.ContainsKey(row.Name))
                {
                    StyleEntity entity = QSheet.RowStyles[row.Name];
                    Range countRange = workSheet.get_Range(workSheet.Cells[startRow + columns.Depth + row.Index, startColumn], workSheet.Cells[startRow + columns.Depth + row.Index, StartColumn + this.columns.Count - 1]);
                    if (entity.BgColor.ToArgb() != Color.FromArgb(0, 0, 0, 0).ToArgb())
                    {
                        countRange.Interior.Color = ColorTranslator.ToOle(entity.BgColor);// string.Format("{0},{1},{2},{3}", entity.BgColor.A, entity.BgColor.R, entity.BgColor.B, entity.BgColor.G);//entity.BgColor.B entity.BgColor.ToArgb();
                    }

                    if (entity.FontColor.ToArgb() != Color.FromArgb(0, 0, 0, 0).ToArgb())
                    {
                        countRange.Font.Color = ColorTranslator.ToOle(entity.FontColor);// string.Format("{0},{1},{2},{3}", entity.BgColor.A, entity.BgColor.R, entity.BgColor.B, entity.BgColor.G);
                    }

                    countRange.Font.Bold = entity.IsBold;
                }
                SetRowBgColor(row.LeafRows, workSheet);
            }
        }       
    }
}
